Juan Andrés Cabral

Home

Research

Code

Media Coverage

Resources



R

# Check if a column contains a specific string
grepl("word",data$column)


# Delete specific rows
myData <- myData[-c(2, 4, 6), ]


# Remove duplicated rows
library(dplyr)
distances_without_na<-distinct(distances_without_na)


# Sample random rows from a dataset
df[sample(nrow(df), 3), ] # 3 rows from df dataset


# Basic function
function_name <- function(arg_1, arg_2, ...) {
#Function body }


# Default value in function
fooBar <- function(x,y){
if(missing(y)) {
x
} else {
x + y
}
}


# Remove duplicated rows
library(dplyr)
distances_without_na<-distinct(distances_without_na)


# New dataframe to store results
newdf <- matrix(0, ncol = 2, nrow = 0)
newdf<-data.frame(newdf)
colnames(newdf)<-
c("col1","col2")


# Select the row with mini or max value
df[which.max(df$col),]
df[which.min(df$col),]


ggplot2

# Vertical line on specific date in ggplot
geom_vline(xintercept = as.Date("2017-01-01"),linetype=2,color="red",size=1)